package com.hpf.springcloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.hpf.commons.utils.CommonResult;
import com.hpf.springcloud.handle.MyFlowExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description:
 * @datetime: 2020/10/31 17:59
 * @author: huangpengfei
 */
@RestController
public class RateLimitController {


    @GetMapping("/byResource")
    @SentinelResource(value = "byResource",blockHandler = "handleException")
    public CommonResult byResource(){
        return new CommonResult(200,"by Resource success","hello world!");
    }

    public CommonResult handleException(BlockException e){
        return new CommonResult(500,"by Resource fail","bye bye world!");
    }

    @GetMapping("/byUrl")
    @SentinelResource(value = "byUrl")
    public CommonResult byUrl(){
        return new CommonResult(200,"by Url success","hello world!");
    }

    @GetMapping("/resource")
    @SentinelResource(value = "resource",exceptionsToIgnore = {IllegalArgumentException.class},blockHandlerClass = MyFlowExceptionHandler.class,blockHandler = "handleExceptionResource")
    public CommonResult resource(){
        return new CommonResult(200,"by Resource success","hello world!");
    }

    @GetMapping("/url")
    @SentinelResource(value = "url",blockHandlerClass = MyFlowExceptionHandler.class,blockHandler = "handleExceptionUrl")
    public CommonResult url(){
        return new CommonResult(200,"by Url success","hello world!");
    }
}
